<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem :label="t('wms.products.uuid')" type="string" v-model="baTable.form.items!.uuid" prop="uuid" :placeholder="t('Please input field', { field: t('wms.products.uuid') })" />
                    <FormItem :label="t('wms.products.shop_id')" type="remoteSelect" v-model="baTable.form.items!.shop_id" prop="shop_id" :input-attr="{ pk: 'ba_.id', field: 'name', 'remote-url': '' }" :placeholder="t('Please select field', { field: t('wms.products.shop_id') })" />
                    <FormItem :label="t('wms.products.brand_id')" type="remoteSelect" v-model="baTable.form.items!.brand_id" prop="brand_id" :input-attr="{ pk: 'ba_.id', field: 'name', 'remote-url': '' }" :placeholder="t('Please select field', { field: t('wms.products.brand_id') })" />
                    <FormItem :label="t('wms.products.category_ids')" type="remoteSelects" v-model="baTable.form.items!.category_ids" prop="category_ids" :input-attr="{ pk: 'ba_.id', field: 'name', 'remote-url': '' }" :placeholder="t('Please select field', { field: t('wms.products.category_ids') })" />
                    <FormItem :label="t('wms.products.title')" type="string" v-model="baTable.form.items!.title" prop="title" :placeholder="t('Please input field', { field: t('wms.products.title') })" />
                    <FormItem :label="t('wms.products.sub_title')" type="string" v-model="baTable.form.items!.sub_title" prop="sub_title" :placeholder="t('Please input field', { field: t('wms.products.sub_title') })" />
                    <FormItem :label="t('wms.products.thumb')" type="string" v-model="baTable.form.items!.thumb" prop="thumb" :placeholder="t('Please input field', { field: t('wms.products.thumb') })" />
                    <FormItem :label="t('wms.products.poster')" type="string" v-model="baTable.form.items!.poster" prop="poster" :placeholder="t('Please input field', { field: t('wms.products.poster') })" />
                    <FormItem :label="t('wms.products.description')" type="string" v-model="baTable.form.items!.description" prop="description" :placeholder="t('Please input field', { field: t('wms.products.description') })" />
                    <FormItem :label="t('wms.products.tags')" type="string" v-model="baTable.form.items!.tags" prop="tags" :placeholder="t('Please input field', { field: t('wms.products.tags') })" />
                    <FormItem :label="t('wms.products.number')" type="number" prop="number" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.number" :placeholder="t('Please input field', { field: t('wms.products.number') })" />
                    <FormItem :label="t('wms.products.ean')" type="string" v-model="baTable.form.items!.ean" prop="ean" :placeholder="t('Please input field', { field: t('wms.products.ean') })" />
                    <FormItem :label="t('wms.products.cost_price')" type="number" prop="cost_price" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.cost_price" :placeholder="t('Please input field', { field: t('wms.products.cost_price') })" />
                    <FormItem :label="t('wms.products.discount_money_price')" type="number" prop="discount_money_price" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.discount_money_price" :placeholder="t('Please input field', { field: t('wms.products.discount_money_price') })" />
                    <FormItem :label="t('wms.products.discount_score_price')" type="number" prop="discount_score_price" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.discount_score_price" :placeholder="t('Please input field', { field: t('wms.products.discount_score_price') })" />
                    <FormItem :label="t('wms.products.discount_magic_point_price')" type="number" prop="discount_magic_point_price" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.discount_magic_point_price" :placeholder="t('Please input field', { field: t('wms.products.discount_magic_point_price') })" />
                    <FormItem :label="t('wms.products.money_price')" type="number" prop="money_price" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.money_price" :placeholder="t('Please input field', { field: t('wms.products.money_price') })" />
                    <FormItem :label="t('wms.products.score_price')" type="number" prop="score_price" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.score_price" :placeholder="t('Please input field', { field: t('wms.products.score_price') })" />
                    <FormItem :label="t('wms.products.magic_point_price')" type="number" prop="magic_point_price" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.magic_point_price" :placeholder="t('Please input field', { field: t('wms.products.magic_point_price') })" />
                    <FormItem :label="t('wms.products.brokerage_money')" type="number" prop="brokerage_money" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.brokerage_money" :placeholder="t('Please input field', { field: t('wms.products.brokerage_money') })" />
                    <FormItem :label="t('wms.products.brokerage_score')" type="number" prop="brokerage_score" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.brokerage_score" :placeholder="t('Please input field', { field: t('wms.products.brokerage_score') })" />
                    <FormItem :label="t('wms.products.second_brokerage_money')" type="number" prop="second_brokerage_money" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.second_brokerage_money" :placeholder="t('Please input field', { field: t('wms.products.second_brokerage_money') })" />
                    <FormItem :label="t('wms.products.second_brokerage_score')" type="number" prop="second_brokerage_score" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.second_brokerage_score" :placeholder="t('Please input field', { field: t('wms.products.second_brokerage_score') })" />
                    <FormItem :label="t('wms.products.reward_money')" type="number" prop="reward_money" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.reward_money" :placeholder="t('Please input field', { field: t('wms.products.reward_money') })" />
                    <FormItem :label="t('wms.products.reward_score')" type="number" prop="reward_score" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.reward_score" :placeholder="t('Please input field', { field: t('wms.products.reward_score') })" />
                    <FormItem :label="t('wms.products.carriage_type')" type="number" prop="carriage_type" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.carriage_type" :placeholder="t('Please input field', { field: t('wms.products.carriage_type') })" />
                    <FormItem :label="t('wms.products.carriage_template_id')" type="remoteSelect" v-model="baTable.form.items!.carriage_template_id" prop="carriage_template_id" :input-attr="{ pk: 'ba_.id', field: 'name', 'remote-url': '' }" :placeholder="t('Please select field', { field: t('wms.products.carriage_template_id') })" />
                    <FormItem :label="t('wms.products.is_show_stock')" type="number" prop="is_show_stock" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.is_show_stock" :placeholder="t('Please input field', { field: t('wms.products.is_show_stock') })" />
                    <FormItem :label="t('wms.products.is_show_sales')" type="number" prop="is_show_sales" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.is_show_sales" :placeholder="t('Please input field', { field: t('wms.products.is_show_sales') })" />
                    <FormItem :label="t('wms.products.is_recommend')" type="number" prop="is_recommend" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.is_recommend" :placeholder="t('Please input field', { field: t('wms.products.is_recommend') })" />
                    <FormItem :label="t('wms.products.is_listed')" type="number" prop="is_listed" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.is_listed" :placeholder="t('Please input field', { field: t('wms.products.is_listed') })" />
                    <FormItem :label="t('wms.products.list_weight')" type="number" prop="list_weight" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.list_weight" :placeholder="t('Please input field', { field: t('wms.products.list_weight') })" />
                    <FormItem :label="t('wms.products.stock')" type="number" prop="stock" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.stock" :placeholder="t('Please input field', { field: t('wms.products.stock') })" />
                    <FormItem :label="t('wms.products.sales')" type="number" prop="sales" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.sales" :placeholder="t('Please input field', { field: t('wms.products.sales') })" />
                    <FormItem :label="t('wms.products.status')" type="number" prop="status" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.status" :placeholder="t('Please input field', { field: t('wms.products.status') })" />
                    <FormItem :label="t('wms.products.cover_chip_status')" type="radio" v-model="baTable.form.items!.cover_chip_status" prop="cover_chip_status" :data="{ content: {} }" :placeholder="t('Please select field', { field: t('wms.products.cover_chip_status') })" />
                    <FormItem :label="t('wms.products.is_offline_useable')" type="number" prop="is_offline_useable" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.is_offline_useable" :placeholder="t('Please input field', { field: t('wms.products.is_offline_useable') })" />
                    <FormItem :label="t('wms.products.is_free_carriage')" type="number" prop="is_free_carriage" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.is_free_carriage" :placeholder="t('Please input field', { field: t('wms.products.is_free_carriage') })" />
                    <FormItem :label="t('wms.products.is_need_address')" type="number" prop="is_need_address" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.is_need_address" :placeholder="t('Please input field', { field: t('wms.products.is_need_address') })" />
                    <FormItem :label="t('wms.products.remark')" type="string" v-model="baTable.form.items!.remark" prop="remark" :placeholder="t('Please input field', { field: t('wms.products.remark') })" />
                    <FormItem :label="t('wms.products.published_at')" type="datetime" v-model="baTable.form.items!.published_at" prop="published_at" :placeholder="t('Please select field', { field: t('wms.products.published_at') })" />
                    <FormItem :label="t('wms.products.deleted_at')" type="datetime" v-model="baTable.form.items!.deleted_at" prop="deleted_at" :placeholder="t('Please select field', { field: t('wms.products.deleted_at') })" />
                    <FormItem :label="t('wms.products.created_at')" type="datetime" v-model="baTable.form.items!.created_at" prop="created_at" :placeholder="t('Please select field', { field: t('wms.products.created_at') })" />
                    <FormItem :label="t('wms.products.updated_at')" type="datetime" v-model="baTable.form.items!.updated_at" prop="updated_at" :placeholder="t('Please select field', { field: t('wms.products.updated_at') })" />
                    <FormItem :label="t('wms.products.image_tag_id')" type="remoteSelect" v-model="baTable.form.items!.image_tag_id" prop="image_tag_id" :input-attr="{ pk: 'ba_.id', field: 'name', 'remote-url': '' }" :placeholder="t('Please select field', { field: t('wms.products.image_tag_id') })" />
                    <FormItem :label="t('wms.products.is_presell')" type="number" prop="is_presell" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.is_presell" :placeholder="t('Please input field', { field: t('wms.products.is_presell') })" />
                    <FormItem :label="t('wms.products.presell_date')" type="datetime" v-model="baTable.form.items!.presell_date" prop="presell_date" :placeholder="t('Please select field', { field: t('wms.products.presell_date') })" />
                    <FormItem :label="t('wms.products.group_price_title')" type="string" v-model="baTable.form.items!.group_price_title" prop="group_price_title" :placeholder="t('Please input field', { field: t('wms.products.group_price_title') })" />
                    <FormItem :label="t('wms.products.options')" type="string" v-model="baTable.form.items!.options" prop="options" :placeholder="t('Please input field', { field: t('wms.products.options') })" />
                    <FormItem :label="t('wms.products.carriage')" type="number" prop="carriage" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.carriage" :placeholder="t('Please input field', { field: t('wms.products.carriage') })" />
                    <FormItem :label="t('wms.products.marked_at')" type="datetime" v-model="baTable.form.items!.marked_at" prop="marked_at" :placeholder="t('Please select field', { field: t('wms.products.marked_at') })" />
                    <FormItem :label="t('wms.products.presell_mode')" type="number" prop="presell_mode" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.presell_mode" :placeholder="t('Please input field', { field: t('wms.products.presell_mode') })" />
                    <FormItem :label="t('wms.products.order_reward')" type="string" v-model="baTable.form.items!.order_reward" prop="order_reward" :placeholder="t('Please input field', { field: t('wms.products.order_reward') })" />
                    <FormItem :label="t('wms.products.supplier')" type="string" v-model="baTable.form.items!.supplier" prop="supplier" :placeholder="t('Please input field', { field: t('wms.products.supplier') })" />
                    <FormItem :label="t('wms.products.search_key')" type="string" v-model="baTable.form.items!.search_key" prop="search_key" :placeholder="t('Please input field', { field: t('wms.products.search_key') })" />
                    <FormItem :label="t('wms.products.category1_id')" type="remoteSelect" v-model="baTable.form.items!.category1_id" prop="category1_id" :input-attr="{ pk: 'ba_.id', field: 'name', 'remote-url': '' }" :placeholder="t('Please select field', { field: t('wms.products.category1_id') })" />
                    <FormItem :label="t('wms.products.category2_id')" type="remoteSelect" v-model="baTable.form.items!.category2_id" prop="category2_id" :input-attr="{ pk: 'ba_.id', field: 'name', 'remote-url': '' }" :placeholder="t('Please select field', { field: t('wms.products.category2_id') })" />
                    <FormItem :label="t('wms.products.line_money_price')" type="number" prop="line_money_price" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.line_money_price" :placeholder="t('Please input field', { field: t('wms.products.line_money_price') })" />
                    <FormItem :label="t('wms.products.line_score_price')" type="number" prop="line_score_price" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.line_score_price" :placeholder="t('Please input field', { field: t('wms.products.line_score_price') })" />
                    <FormItem :label="t('wms.products.admin_id')" type="remoteSelect" v-model="baTable.form.items!.admin_id" prop="admin_id" :input-attr="{ pk: 'ba_.id', field: 'name', 'remote-url': '' }" :placeholder="t('Please select field', { field: t('wms.products.admin_id') })" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    number: [buildValidatorData({ name: 'number', title: t('wms.products.number') })],
    cost_price: [buildValidatorData({ name: 'number', title: t('wms.products.cost_price') })],
    discount_money_price: [buildValidatorData({ name: 'number', title: t('wms.products.discount_money_price') })],
    discount_score_price: [buildValidatorData({ name: 'number', title: t('wms.products.discount_score_price') })],
    discount_magic_point_price: [buildValidatorData({ name: 'number', title: t('wms.products.discount_magic_point_price') })],
    money_price: [buildValidatorData({ name: 'number', title: t('wms.products.money_price') })],
    score_price: [buildValidatorData({ name: 'number', title: t('wms.products.score_price') })],
    magic_point_price: [buildValidatorData({ name: 'number', title: t('wms.products.magic_point_price') })],
    brokerage_money: [buildValidatorData({ name: 'number', title: t('wms.products.brokerage_money') })],
    brokerage_score: [buildValidatorData({ name: 'number', title: t('wms.products.brokerage_score') })],
    second_brokerage_money: [buildValidatorData({ name: 'number', title: t('wms.products.second_brokerage_money') })],
    second_brokerage_score: [buildValidatorData({ name: 'number', title: t('wms.products.second_brokerage_score') })],
    reward_money: [buildValidatorData({ name: 'number', title: t('wms.products.reward_money') })],
    reward_score: [buildValidatorData({ name: 'number', title: t('wms.products.reward_score') })],
    carriage_type: [buildValidatorData({ name: 'number', title: t('wms.products.carriage_type') })],
    is_show_stock: [buildValidatorData({ name: 'number', title: t('wms.products.is_show_stock') })],
    is_show_sales: [buildValidatorData({ name: 'number', title: t('wms.products.is_show_sales') })],
    is_recommend: [buildValidatorData({ name: 'number', title: t('wms.products.is_recommend') })],
    is_listed: [buildValidatorData({ name: 'number', title: t('wms.products.is_listed') })],
    list_weight: [buildValidatorData({ name: 'number', title: t('wms.products.list_weight') })],
    stock: [buildValidatorData({ name: 'number', title: t('wms.products.stock') })],
    sales: [buildValidatorData({ name: 'number', title: t('wms.products.sales') })],
    status: [buildValidatorData({ name: 'number', title: t('wms.products.status') })],
    is_offline_useable: [buildValidatorData({ name: 'number', title: t('wms.products.is_offline_useable') })],
    is_free_carriage: [buildValidatorData({ name: 'number', title: t('wms.products.is_free_carriage') })],
    is_need_address: [buildValidatorData({ name: 'number', title: t('wms.products.is_need_address') })],
    published_at: [buildValidatorData({ name: 'date', title: t('wms.products.published_at') })],
    deleted_at: [buildValidatorData({ name: 'date', title: t('wms.products.deleted_at') })],
    created_at: [buildValidatorData({ name: 'date', title: t('wms.products.created_at') })],
    updated_at: [buildValidatorData({ name: 'date', title: t('wms.products.updated_at') })],
    is_presell: [buildValidatorData({ name: 'number', title: t('wms.products.is_presell') })],
    presell_date: [buildValidatorData({ name: 'date', title: t('wms.products.presell_date') })],
    carriage: [buildValidatorData({ name: 'number', title: t('wms.products.carriage') })],
    marked_at: [buildValidatorData({ name: 'date', title: t('wms.products.marked_at') })],
    presell_mode: [buildValidatorData({ name: 'number', title: t('wms.products.presell_mode') })],
    line_money_price: [buildValidatorData({ name: 'number', title: t('wms.products.line_money_price') })],
    line_score_price: [buildValidatorData({ name: 'number', title: t('wms.products.line_score_price') })],
})
</script>

<style scoped lang="scss"></style>
